<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>后台管理系统</title>
    <meta name="author" content="BaiYY"/>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
    <meta name="renderer" content="webkit">
    <link rel="stylesheet" type="text/css" href="__PUBLIC__/admin/css/bootstrap.min.css"/>
    <link rel="stylesheet" type="text/css" href="__PUBLIC__/admin/css/Font/css/font-awesome.min.css"/>
    <link rel="stylesheet" type="text/css" href="__PUBLIC__/admin/css/index.css"/>
    <script type="text/javascript" src="__PUBLIC__/admin/js/jquery.js"></script>
    <script type="text/javascript" src="__PUBLIC__/admin/js/bootstrap.min.js"></script>
    <script type="text/javascript" src="__PUBLIC__/admin/js/index.js"></script>
    <style>
       table tr td input{
           border: 1px solid #dddddd;
        }
    </style>
</head>
<body>
    <div class="right-sidebar" style="max-width: 1200px">
        <ul class="nav nav-tabs" style="margin-bottom: 16px" role="tablist" id="listTab">
            <li role="presentation"><a href="initSet.html">{:L(GAME_SETTING_INIT)}</a></li>
            <li role="presentation" id="invited"><a href="activityInvite.html">{:L(GAME_SETTING_INVITE)}</a></li>
            <li role="presentation" id="roomd"><a href="activityCreateRoom.html">{:L(GAME_SETTING_ROOM)}</a></li>
            <li role="presentation" id="advd"><a href="advSet.html">{:L(GAME_SETTING_ADV)}</a></li>
            <li role="presentation" id="wheeld" class="active"><a href="luckWheel.html">{:L(GAME_SERRING_WHEEL)}</a></li>
        </ul>
        <div>
            <p><strong>说明:</strong></p>
            <p>所有的幸运概率加起来是10000</p>
        </div>

        <div class="table-responsive">
                <table class="table text-center">
                    <thead>
                    <tr>
                        <th>奖励等级</th>
                        <th>幸运概率</th>
                        <th id="showcard">{:L(WHEEL_GAME_RECARD)}</th>
                        <th id="showcoin">{:L(WHEEL_GAME_RECOIN)}</th>
                        <th>操作</th>
                    </tr>
                    </thead>
                    <tbody id="dataList"></tbody>
                </table>
                <div class="text-center">
                    <button class="btn btn-primary" id="sbtn">提交修改</button>
                </div>
        </div>
    </div>
    <script type="text/javascript">
        $(function () {
            var invited = $.trim($("#invited").text());
            var roomd = $.trim($("#roomd").text());
            var advd = $.trim($("#advd").text());
            var wheeld = $.trim($("#wheeld").text());

            if(invited == '' || invited == 'GAME_SETTING_INVITE') {
                $("#invited").hide();
            }
            if(roomd == '' || roomd == 'GAME_SETTING_ROOM') {
                $("#roomd").hide();
            }
            if(advd == '' || advd == 'GAME_SETTING_ADV') {
                $("#advd").hide();
            }

            if(wheeld == '' || wheeld == 'GAME_SERRING_WHEEL') {
                $("#wheeld").hide();
            }

            var vshowcard = $.trim($("#showcard").text());
            var vshowcoin = $.trim($("#showcoin").text());
            if(vshowcard == '' || vshowcard == null) {
                $("#showcard").hide();
            }
            if(vshowcoin == '' || vshowcoin == null) {
                $("#showcoin").hide();
            }
            $.ajax({
                type:'post',
                url:"{:U('Admin/Main/getLuckWheel')}",
                async:false,
                success:function (data) {
//                    console.log(data);
                    data = Object.keys(data).map(function (k) {
                        return data[k];
                    });
                    if(data == 'error') {
                        alert("请求资源失败！");
                    }else{
                        var str = '';
                        for(var i=0;i<data.length;++i) {
                            if($("#showcard").text() == '' && $("#showcoin").text() !='') {
                                str += "<tr><td>"+(i*1+1)+"等奖</td><td><input name='mark' value='"+(i*1+1)+"' id='mark"+(i*1+1)+"' type='hidden'/><input type='number' name='weight' class='form-control' id=weight"+(i*1+1)+" disabled='true' min='0' max='10000' value='"+data[i].weight+"'/></td><td style='display: none;'><input type='hidden'class='form-control' id='card"+(i*1+1)+"' name='card' disabled='true' min='0' max='10000' value='"+data[i].resource.card+"'/></td><td><input type='number' id='coin"+(i*1+1)+"' disabled='true' class='form-control' name='coin' min='0' value='"+data[i].resource.coin+"'/></td><td><button class='btn btn-primary' onclick='onClick(this)'>修改</button></td></tr> "
                            }else if($("#showcard").text() != '' && $("#showcoin").text() =='') {
                                str += "<tr><td>"+(i*1+1)+"等奖</td><td><input name='mark' value='"+(i*1+1)+"' id='mark"+(i*1+1)+"' type='hidden'/><input type='number' name='weight' class='form-control' id=weight"+(i*1+1)+" disabled='true' min='0' max='10000' value='"+data[i].weight+"'/></td><td><input type='number'class='form-control' id='card"+(i*1+1)+"' name='card' disabled='true' min='0' value='"+data[i].resource.card+"'/></td><td style='display: none;'><input type='hidden' id='coin"+(i*1+1)+"' disabled='true' class='form-control' name='coin' min='0' value='"+data[i].resource.coin+"'/></td><td><button class='btn btn-primary' onclick='onClick(this)'>修改</button></td></tr> "
                            }else if(($("#showcard").text() == '') && ($("#showcoin").text() =='')){
                                str += "<tr><td>"+(i*1+1)+"等奖</td><td><input name='mark' value='"+(i*1+1)+"' id='mark"+(i*1+1)+"' type='hidden'/><input type='number' name='weight' class='form-control' id=weight"+(i*1+1)+" disabled='true' min='0' max='10000' value='"+data[i].weight+"'/></td><td style='display: none'><input type='hidden'class='form-control' id='card"+(i*1+1)+"' name='card' disabled='true' min='0' value='"+data[i].resource.card+"'/></td><td style='display: none'><input type='hidden' id='coin"+(i*1+1)+"' disabled='true' class='form-control' name='coin' min='0' value='"+data[i].resource.coin+"'/></td><td><button class='btn btn-primary' onclick='onClick(this)'>修改</button></td></tr> "
                            }else{
                                str += "<tr><td>"+(i*1+1)+"等奖</td><td><input name='mark' value='"+(i*1+1)+"' id='mark"+(i*1+1)+"' type='hidden'/><input type='number' name='weight' class='form-control' id=weight"+(i*1+1)+" disabled='true' min='0' max='10000' value='"+data[i].weight+"'/></td><td><input type='number'class='form-control' id='card"+(i*1+1)+"' name='card' disabled='true' min='0' value='"+data[i].resource.card+"'/></td><td><input type='number' id='coin"+(i*1+1)+"' disabled='true' class='form-control' name='coin' min='0' value='"+data[i].resource.coin+"'/></td><td><button class='btn btn-primary' onclick='onClick(this)'>修改</button></td></tr> "
                            }
                        }
                        $("#dataList").html(str);
                    }
                }
            });
            $(":input").keyup(function () {
                if($(this).val()*1 < 0 ) {
                    alert("输入的值不能为负");
                    $(this).val('');
                }
            })
        });


        function onClick(This) {
            var num = $(This).closest("tr").find(":hidden").val();
            $(This).closest("tr").find("input").attr("disabled",false);
        }

        $("#sbtn").on("click",function () {
            var weight1 = $("#weight1").val();
            var weight2 = $("#weight2").val();
            var weight3 = $("#weight3").val();
            var weight4 = $("#weight4").val();
            var weight5 = $("#weight5").val();
            var weight6 = $("#weight6").val();
            var weight7 = $("#weight7").val();
            var card1 = $("#card1").val();
            var card2 = $("#card2").val();
            var card3 = $("#card3").val();
            var card4 = $("#card4").val();
            var card5 = $("#card5").val();
            var card6 = $("#card6").val();
            var card7 = $("#card7").val();
            var coin1 = $("#coin1").val();
            var coin2 = $("#coin2").val();
            var coin3 = $("#coin3").val();
            var coin4 = $("#coin4").val();
            var coin5 = $("#coin5").val();
            var coin6 = $("#coin6").val();
            var coin7 = $("#coin7").val();

            var maxnum = weight1/1 + weight2/1 + weight3/1+weight4/1+weight5/1+weight6/1+weight7/1;

            if(maxnum > 10000 || maxnum < 10000) {
                alert("概率加起来必须是10000");
                window.location.href = "{:U('Admin/Main/luckWheel')}";
                return false;
            }
//            console.log(maxnum);
            var params = {
                "1":{"weight":weight1,"resource":{"card":card1,"coin":coin1}},
                "2":{"weight":weight2,"resource":{"card":card2,"coin":coin2}},
                "3":{"weight":weight3,"resource":{"card":card3,"coin":coin3}},
                "4":{"weight":weight4,"resource":{"card":card4,"coin":coin4}},
                "5":{"weight":weight5,"resource":{"card":card5,"coin":coin5}},
                "6":{"weight":weight6,"resource":{"card":card6,"coin":coin6}},
                "7":{"weight":weight7,"resource":{"card":card7,"coin":coin7}}
            }
            $.ajax({
               type:"post",
               url:"{:U('Admin/Main/setLuckWheel')}",
               dataType:"json",
               data:{"params":params},
               success:function (data) {
                   if(data == 'success') {
                       alert("设置成功！")
                       window.location.href = "{:U('Admin/Main/luckWheel')}";
                   }else if(data == 'error'){
                        alert("设置失败！");
                   }
               }
           })
        })
    </script>
</body>
</html>